using AvalonAdminDto.Common;
using CommunityToolkit.Mvvm.Input;
using D.UtilCore.Dtos;
using Irihi.Avalonia.Shared.Contracts;
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Input;
using Ursa.Controls;

namespace AvalonAdmin.BaseViewModel
{
    /// <summary>
    /// 基础新增，修改视图模型
    /// </summary>
    public abstract class BaseModifyViewModel : Base, IDialogContext
    {
        #region 命令
        /// <summary>
        /// 提交命令
        /// </summary>
        public ICommand SubmitCommand { get; protected set; }

        /// <summary>
        /// 重置表单命令
        /// </summary>
        public ICommand ResetFormCommand { get; protected set; }

        /// <summary>
        /// 重置命令
        /// </summary>
        public ICommand ResetCommand { get; protected set; }

        /// <summary>
        /// 获取命令
        /// </summary>
        public RelayCommand<string> GetCommand { get; protected set; }

        #endregion

        #region 部分属性启用，显示
        private bool _isEnabled = false;

        /// <summary>
        /// 是否启用/禁用
        /// </summary>
        public bool IsEnabled
        {
            get => _isEnabled;
            set => SetProperty(ref _isEnabled, value);
        }

        private bool _isEditVisibility = true;
        /// <summary>
        /// 业务不可修改的字段是否显示，默认显示
        /// </summary>
        public bool IsEditVisibility
        {
            get => _isEditVisibility;
            set => SetProperty(ref _isEditVisibility, value);
        }

        private bool _isHistoryVisibility = false;
        /// <summary>
        /// 历史操作信息是否显示，默认不显示
        /// </summary>
        public bool IsHistoryVisibility
        {
            get => _isHistoryVisibility;
            set => SetProperty(ref _isHistoryVisibility, value);
        }

        public void SetupFormStatus(AvalonAdminDto.Common.ViewStatus? viewStatus)
        {
            if (viewStatus == ViewStatus.View)
            {// 查看详情
                IsEnabled = false; //禁用所有输入
                IsEditVisibility = true; // 所有都显示
                IsHistoryVisibility = true;
            }
            else if (viewStatus == ViewStatus.Add)
            {// 新增
                IsEnabled = true; // 所有显示的都可以输入
                IsEditVisibility = true;// 所有都显示
                IsHistoryVisibility = false;// 其他部分字段不显示
            }
            else
            {// 修改
                IsEnabled = true; // 所有显示的都可以输入
                IsEditVisibility = false;// 部分不能显示(密码，二次密码)
                IsHistoryVisibility = false;// 其他部分字段不显示
            }
        }

        #endregion

        #region 构造函数
        protected BaseModifyViewModel()
        {
            // 初始化命令
            SubmitCommand = new AsyncRelayCommand(SubmitAsync);
            ResetCommand = new RelayCommand(() => Reset());
        }
        #endregion

        #region 抽象方法
        /// <summary>
        /// 提交数据
        /// </summary>
        /// <returns></returns>
        protected abstract Task SubmitAsync();

        /// <summary>
        /// 重置表单
        /// </summary>
        protected abstract void Reset();

        /// <summary>
        /// 处理API响应
        /// </summary>
        protected async void HandleApiResponse(ApiResult<int> res, string successMsg, string failMsg)
        {
            if (res.Code == StatusCodeEnum.OK)
            {
                // 如果NotificationManager为null，使用MessageBox显示消息
                if (NotificationManager == null)
                {
                    await ShowMsgSuccess(successMsg);
                }
                else
                {
                    ShowSuccess(successMsg);
                }
                CloseDialog(DialogResult.OK);
            }
            else if (res.Code == StatusCodeEnum.Waring)
            {
                // 如果NotificationManager为null，使用MessageBox显示消息
                if (NotificationManager == null)
                {
                    await ShowMsgWarning(res.Msg);
                }
                else
                {
                    ShowWarning(res.Msg);
                }
            }
            else
            {
                // 如果NotificationManager为null，使用MessageBox显示消息
                if (NotificationManager == null)
                {
                    await ShowMsgError(res.Msg ?? failMsg);
                }
                else
                {
                    ShowError(res.Msg ?? failMsg);
                }
            }
        }
        #endregion

        #region IDialogContext 实现
        public event EventHandler<object?>? RequestClose;

        public virtual Task<bool> QueryAsync()
        {
            return Task.FromResult(true);
        }

        public void Close()
        {
            RequestClose?.Invoke(this, DialogResult.Cancel);
        }

        protected void CloseDialog(DialogResult result = DialogResult.OK)
        {
            RequestClose?.Invoke(this, result);
        }
        #endregion
    }
}